<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>查看所有签署者</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            padding-top: 60px;
            background-color: #f8f9fa;
        }
        .form-container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        table th, table td {
            vertical-align: middle;
        }
        .no-data {
            margin-top: 20px;
            color: #6c757d;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <div class="container">
        <a class="navbar-brand" href="index.html">DCO 签署系统</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
</nav>
<br>

<!-- 主内容 -->
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="form-container">
                <h2 class="mb-4 text-center">查看所有签署者</h2>
                <form id="signaturesForm" class="mb-4">
                    <div class="input-group">
                        <input type="text" class="form-control" id="repoUrl" name="repoUrl" placeholder="输入AtomGit仓库URL" required>
                        <button class="btn btn-primary" type="submit">获取签署者列表</button>
                    </div>
                </form>
                <div id="signaturesResult"></div>
            </div>
        </div>
    </div>
</div>

<!-- 引入Bootstrap JS和依赖 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    document.getElementById('signaturesForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const repoUrl = document.getElementById('repoUrl').value;

        fetch(`/api/dco/signatures?repoUrl=${encodeURIComponent(repoUrl)}`)
            .then(response => response.json().then(data => ({status: response.status, body: data})))
            .then(result => {
                const resultDiv = document.getElementById('signaturesResult');
                if (result.status === 200) {
                    if (result.body.length > 0) {
                        let table = `<table class="table table-striped table-bordered">
                                            <thead class="table-dark">
                                                <tr>
                                                    <th>ID</th>
                                                    <th>仓库 URL</th>
                                                    <th>姓名</th>
                                                    <th>邮箱</th>
                                                    <th>签署时间</th>
                                                </tr>
                                            </thead>
                                            <tbody>`;
                        result.body.forEach(signature => {
                            table += `<tr>
                                            <td>${signature.id}</td>
                                            <td>${signature.repoUrl}</td>
                                            <td>${signature.name}</td>
                                            <td>${signature.email}</td>
                                            <td>${new Date(signature.signedAt).toLocaleString()}</td>
                                          </tr>`;
                        });
                        table += `</tbody></table>`;
                        resultDiv.innerHTML = table;
                    } else {
                        resultDiv.innerHTML = '<div class="no-data">没有找到签署记录。</div>';
                    }
                } else {
                    const errorMessage = result.body.message || '未知错误';
                    resultDiv.innerHTML = `<div class="alert alert-danger">错误: ${errorMessage}</div>`;
                }
            })
            .catch(error => {
                const resultDiv = document.getElementById('signaturesResult');
                resultDiv.innerHTML = `<div class="alert alert-danger">请求失败: ${error}</div>`;
            });
    });
</script>
</body>
</html>
